<?php

/**
 * 后台分类管理控制器
 */
class CategoryController extends PlatformController {
	/**
	 * 显示分类管理首页
	 */
	public function indexAction() {
		//操作模型，提取所有分类的信息
		$category = Factory::M('CategoryModel');
		$cateInfo = $category->getCategory();
		$this->assign('cateInfo',$cateInfo);
		$this->display('index.html');
	}
	/**
	 * 显示添加分类表单
	 */
	public function addAction() {
		// 提取分类信息
		$category = Factory::M('CategoryModel');
		$cateInfo = $category->getCategory();
		// 分配变量到视图文件
		$this->assign('cateInfo', $cateInfo);
		// 输出视图
		$this->display('add.html');
	}
	public function dealAddAction() {
		//接收数据
		$cate = array();
		$cate['cate_name'] = $this->escapeData($_POST['cate_name']);
		$cate['cate_pid'] = $_POST['cate_pid'];
		$cate['cate_sort'] = $this->escapeData($_POST['cate_sort']);
		$cate['cate_desc'] = $this->escapeData($_POST['cate_desc']);
		//判断数据的合法性
		if(empty($cate['cate_name']) || empty($cate['cate_sort']) || empty($cate['cate_desc'])) {
			$this->jump('index.php?p=Back&c=Category&a=add', ':( 信息不完整!');
			if(!is_numeric($cate['cate_sort']) || (int)$cate['cate_sort'] != $cate['cate_sort'] || $cate['cate_sort'] < 1){
				$this->jump('index.php?p=Back&c=Category&a=Add', ':( 排列的序号应该为1-50');
			}
		}
		//数据入库，操作模型
		$category = Factory::M('CategoryModel');
		//调用inserCate方法
		$result = $category->insertCate($cate);
		//跳转分页
		if($result) {
			$this->jump('index.php?p=Back&c=Category&a=index', "类名为：\"{$cate['cate_name']}\"添加成功");
		}else {
			$this->jump('index.php?p=Back&c=Category&a=Add', ':( 发生未知错误，添加失败！');
		}
	}
	/**
	 * 展示修改分类的表单
	 */
	public function editAction() {
		//先获取当前分类的ID号
		$cate_id = $_GET['cate_id'];
		//根据ID号提取当前分类的相关信息
		$category = Factory::M('CategoryModel');
		$cate = $category->getCategoryById($cate_id);
		//分配变量
		$this->assign('cate',$cate);
		//页面中依然要显示所有类别，所以还是要提取所有的分类的信息
		$cateInfo = $category->getCategory();
		$this->assign('cateInfo',$cateInfo);
		$this->display('edit.html');
	}
	/**
	 * 处理修改分类表单的动作
	 */
	public function dealEditAction() {
		//接收数据
		$cate = array();
		$cate['cate_name'] = $this->escapeData($_POST['cate_name']);
		$cate['cate_pid'] = $_POST['cate_pid'];
		$cate['cate_sort'] = $this->escapeData($_POST['cate_sort']);
		$cate['cate_desc'] = $this->escapeData($_POST['cate_desc']);
		$cate['cate_id'] = $_POST['cate_id'];//在隐藏域
		//判断数据的合法性
		if(empty($cate['cate_name']) || empty($cate['cate_sort']) || empty($cate['cate_desc'])) {
			$this->jump("index.php?p=Back&c=Category&a=edit&cate_id={$cate['cate_id']}", ':( 信息不完整!');
			if(!is_numeric($cate['cate_sort']) || (int)$cate['cate_sort'] != $cate['cate_sort'] || $cate['cate_sort'] < 1){
				$this->jump("index.php?p=Back&c=Category&a=edit&cate_id={$cate['cate_id']} ", ':( 排列的序号应该为1-50');
			}
		}
		//数据入库，操作模型
		$category = Factory::M('CategoryModel');
		//调用inserCate方法
		$result = $category->updateCateById($cate);
		//跳转分类页
		if($result) {
			$this->jump('index.php?p=Back&c=Category&a=index', "{$cate['cate_name']}更新成功,新的排序为{$cate['cate_id']}");
		}else {
			$this->jump("index.php?p=Back&c=Category&a=edit&cate_id={$cate['cate_id']}",':( 发生未知错误，修改失败！');
		}
		
	}
	/**
	 * 删除某条指定分类的动作
	 */
	public function delAction() {
		//接受表单
		$cate_id = $_GET['cate_id'];
		//判断该分类是否可以被删除
		$category = Factory::M('CategoryModel');
		$subId = $category->getSubId($cate_id);
		if($subId) {
			//说明存在子分类不能删除
			$this->jump('index.php?p=Back&c=Category&a=index', ':( 该分类存在子分类，不能被删除！');
		}
		//开始删除
		$result = $category->delCategoryById($cate_id);
		if($result) {
			$this->jump('index.php?p=Back&c=Category&a=index',"删除成功！");
		}else {
			$this->jump('index.php?p=Back&c=Category&a=index', ":( 发生未知错误，删除失败！");
		}
	}
	/**
	 * 批量删除分类动作
	 */
	public function delAllAction() {
		//先判断用户有没有勾选
		if(!isset($_POST['cate_id'])) {
			$this->jump('index.php?p=Back&c=Category&a=index', ':( 请选中要删除的分类');
		}
		//接收要勾选的ID号
		$cate_id = $_POST['cate_id'];//接收是个数组，多个ID号
		$category = Factory::M('CategoryModel');
		foreach($cate_id as $id) {
			if($category->getSubId($id)){
				//说明存在子分类，不能删除
				$this->jump('index.php?p=Back&c=Category&a=index', ':( 不能删除存在子分类的分类！');
			}
		}
		//执行批量操作
		$result = $category->delAllCategory($cate_id);
		if($result) {
			$this->jump('index.php?p=Back&c=Category&a=index',"删除成功");
		}else {
			$this->jump('index.php?p=Back&c=Category&a=index', ':( 发生未知错误，批量删除失败！ ');
		}
	}
}